﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using Catering_Rules;
using System.Data;
using Microsoft.Reporting.WebForms;
using System.Text;

namespace Catering.Catering_Page.ShanShiFenXi
{
    public partial class PresonAvgContrant : System.Web.UI.Page
    {
        #region 属性

        private static string areaId = "";//作业区ID
        private static string classId = "";//班组ID
        private static string[] years;//年份
        private static int[] RawId;//食品大类ID
        View_Monitor vm = new View_Monitor();

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        #region 取得页面数据

        /// <summary>
        /// 获取页面数据Type
        /// </summary>
        private void GetPageDataForType()
        {
            areaId = DDLTool1.GetDDL_Area();
            classId = DDLTool1.GetDDL_Class();
            RawId = UCSearchToolByZW1.GetRaw();
            years = UCSearchToolByZW1.GetYears();
        }

        #endregion

        #region 大类转换种属
        private string EXChangeRAW(string RAWNAME)
        {
            switch (RAWNAME)
            {
                case "肉类":
                    RAWNAME = "禽肉类";
                    break;
                case "水产类":
                    RAWNAME = "鱼虾类";
                    break;
                case "蔬菜类":
                    RAWNAME = "蔬菜类";
                    break;
                case "水果类":
                    RAWNAME = "水果类";
                    break;
                case "粮食类":
                    RAWNAME = "谷类薯类及杂豆";
                    break;
            }
            return RAWNAME;
        }
        #endregion

        protected void lbtnTyleChar_Click(object sender, EventArgs e)
        {
            GetPageDataForType();//获取页面参数
            if (years == null || years.Count() < 0 || RawId == null || RawId.Count() <= 0)
            {
                return;
            }
            DataTable frdt = null, dynamicdt = null;//消耗汇总、人数汇总
            Dictionary<string, Dictionary<string, decimal>> zhonglei = new Dictionary<string, Dictionary<string, decimal>>();

            if (years.Length > 1)
            {
                foreach (string tempyear in years)
                {
                    foreach (string rawName in UCSearchToolByZW1.GetRawName())
                    {
                        Dictionary<string, decimal> list = GetReportModel(areaId, classId);//班组列表(x轴列表)
                        if (!zhonglei.ContainsKey(tempyear + "年" + rawName)) zhonglei.Add(tempyear + "年" + rawName, list);
                    }
                }
            }
            else
            {
                foreach (string rawName in UCSearchToolByZW1.GetRawName())
                {
                    Dictionary<string, decimal> list = GetReportModel(areaId, classId);//班组列表(x轴列表)
                    if (!zhonglei.ContainsKey(rawName)) { zhonglei.Add(rawName, list); }
                }
            }


            //获取消耗汇总、人数汇总
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            if (years != null && years.Count() > 0)
            {
                for (int i = 0; i < years.Count(); i++)
                {
                    if (Convert.ToInt32(years[i]) < 2012)
                    {
                        DataTable dt = new Catering_Rules.DataViewForReport().GetPerson_Avg_ContrastByYearHistory(pid, years, areaId, classId, RawId);
                        for (int ii = 0; ii < dt.Rows.Count; ii++)
                        {
                            string yeardate = dt.Rows[ii]["yeardate"].ToString();
                            int classid = Convert.ToInt32(dt.Rows[ii]["classid"]);
                            string className = dt.Rows[ii]["cname"].ToString();
                            decimal SumNumber = Convert.ToInt32(dt.Rows[ii]["SumNumber"]);
                            string typename = EXChangeRAW(dt.Rows[ii]["typename"].ToString());
                            string tempyear = years.Length > 0 ? yeardate + "年" : "";
                            zhonglei[tempyear + typename][className] = SumNumber;
                        }
                    }
                }
            }
            vm.GetPreson_Avg_Contrast(pid, areaId, years, classId, RawId, out frdt, out dynamicdt);
            var renmodel = from c in dynamicdt.AsEnumerable()
                           select new
                           {
                               Number = Convert.ToDecimal(c["Number"]),
                               yearofdate = c["yearofdate"].ToString(),
                               classid = Convert.ToInt32(c["classid"]),
                               CName = c["CName"].ToString()
                           };
            dynamicdt.Dispose();
            if (frdt != null && frdt.Rows.Count > 0 && dynamicdt != null && dynamicdt.Rows.Count > 0)
            {
                for (int i = 0; i < frdt.Rows.Count; i++)
                {
                    string yeardate = frdt.Rows[i]["yeardate"].ToString();
                    int classid = Convert.ToInt32(frdt.Rows[i]["classid"]);
                    string className = frdt.Rows[i]["cname"].ToString();
                    decimal SumNumber = Convert.ToInt32(frdt.Rows[i]["SumNumber"]);
                    string typename = frdt.Rows[i]["typename"].ToString();
                    string tempyear = years.Length > 1 ? yeardate + "年" : "";
                    foreach (var ClassItem in renmodel.Where(p => p.classid == classid && p.yearofdate == yeardate))
                    {
                        if (zhonglei.ContainsKey(tempyear + typename))
                        {
                            zhonglei[tempyear + typename][className] = Toolss.GetMathResult(SumNumber / ClassItem.Number,0, 3);
                            break;
                        }
                    }
                }
                frdt.Dispose();
            }

            string StrX = "";
            string str = "";
            if (zhonglei != null)
            {
                int n = 0;
                StringBuilder Sresult = new StringBuilder("");
                foreach (string raw in zhonglei.Keys)
                {
                    Sresult.Append("{");
                    Sresult.Append("name:'" + raw + "',");
                    Sresult.Append("data: [");
                    string StrData = "";
                    foreach (string key in zhonglei[raw].Keys)
                    {
                        if (n == 0) StrX += "'" + key + "',";
                        StrData += zhonglei[raw][key] + ",";
                    }
                    StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                    Sresult.Append(StrData + "]");
                    if (n == 0) StrX = StrX.Length > 0 ? StrX.Substring(0, StrX.Length - 1) : "";
                    Sresult.Append("},");
                    str = Sresult.ToString();
                    n++;
                }

            }
            StrX = "[" + StrX + "]";
            str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + Title + "'," + StrX + "," + str + ");", true);
        }

        /// <summary>
        /// 获取横坐标值
        /// </summary>
        /// <param name="groupid"></param>
        /// <param name="classid"></param>
        /// <returns></returns>
        public Dictionary<int, string> GetClassList(string groupid, string classid)
        {
            Dictionary<int, string> result = new Dictionary<int, string>();
            if (!string.IsNullOrEmpty(groupid) && !groupid.Equals("-10"))
            {
                if (!string.IsNullOrEmpty(classid) && !classid.Equals("-10"))
                {
                    result.Add(Convert.ToInt32(classid), DDLTool1.GetAllDDL_Class()[Convert.ToInt32(classid)]);
                    return result;
                }
                else
                {
                    return DDLTool1.GetAllDDL_Class();
                }
            }
            else
            {
                Dictionary<int, string> temp = DDLTool1.GetAllDDL_Area();
                List<Catering_Model.ClassInfo> listClass = new Catering_Rules.ClassInfo().GetAllList(0);
                if (temp.Count > 0)
                {
                    foreach (int id in temp.Keys)
                    {
                        foreach (Catering_Model.ClassInfo item in listClass.Where(c => c.AreaID == id))
                        {
                            result.Add(item.id, item.CName);
                        }
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 获取横坐标值,并初始化
        /// </summary>
        /// <param name="groupid"></param>
        /// <param name="classid"></param>
        /// <returns></returns>
        public Dictionary<string, decimal> GetReportModel(string groupid, string classid)
        {
            Dictionary<string, decimal> result = new Dictionary<string, decimal>();
            Dictionary<int, string> temp = GetClassList(groupid, classid);
            foreach (string value in temp.Values)
            {
                if (!result.ContainsKey(value)) result.Add(value, 0);
            }
            return result;
        }
    }
}